﻿using System;
using System.Collections.Generic;
using System.Data;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Providers;

namespace visinia.SmartEngine.Pages
{
    /// <summary>
    /// The PageController class is used to create delete update visinia webpages
    /// Pages are not created delete updated directly in visinia, no direct interface is given. 
    /// But page creation deletion etc are through the visinia Plugins, visinia Plugins can add as many pages
    /// as are required and can update or delete them on will.
    /// </summary>
    public class PageController
    {
        /// <summary>
        /// Adds a new page
        /// </summary>
        /// <param name="PageId">The page to add</param>
        /// <returns>Returns the id of the newly created page</returns>
        public static int AddPage(PageInfo Page)
        {
            IDataReader dr = SmartEngineDataProvider.Provider.AddPage(Page.Name, Page.Title, Page.PluginId);
            int index = 0;
            while (dr.Read())
            {
                if (dr[0] != Convert.DBNull)
                    index = (int)dr[0];
            }
            DataCache.DeleteCacheMultiple("page");            
            dr.Close();
            return index;
        }

        /// <summary>
        /// Deletes the specified page
        /// </summary>
        /// <param name="PageInfo">The page to delete</param>
        public static void DeletePage(PageInfo Page)
        {
            SmartEngineDataProvider.Provider.DeletePage(Page.PageId);
            DataCache.DeleteCacheMultiple("page");            
        }

        /// <summary>
        /// Deletes the specified page
        /// </summary>
        /// <param name="PageId">The page to delete</param>
        public static void DeletePage(int PageId)
        {
            SmartEngineDataProvider.Provider.DeletePage(PageId);
            DataCache.DeleteCacheMultiple("page");
        }

        /// <summary>
        /// Updates the specified page
        /// </summary>
        /// <param name="PageId">The page to update</param>
        public static void UpdatePage(PageInfo Page)
        {
            SmartEngineDataProvider.Provider.UpdatePage(Page.PageId, Page.Name, Page.Title, Page.PluginId);
            DataCache.DeleteCacheMultiple("page");            
        }

        /// <summary>
        /// Gets the specified page by its unique id
        /// </summary>
        /// <param name="PageId">The page id to return</param>
        public static PageInfo GetPage(int PageId)
        {
            return GetPages().Find(p => p.PageId == PageId);
        }

        /// <summary>
        /// Gets the specified page by its name
        /// </summary>
        /// <param name="pageName">The page name to return</param>
        public static PageInfo GetPage(string pageName)
        {
            return GetPages().Find(p => (string.Compare(p.Name, pageName, true) == 0));
        }
                
        /// <summary>
        /// Gets the list of all pages
        /// </summary>
        public static List<PageInfo> GetPages()
        {
            string key = "pages";
            List<PageInfo> pages = (List<PageInfo>)DataCache.GetCache(key);
            if (pages == null)
            {
                pages = FillPages(SmartEngineDataProvider.Provider.GetPages());
                DataCache.SetCache(key, pages, new TimeSpan(0, 30, 0));
            }
            return pages;
        }

        public static List<PageInfo> GetPages(int PluginId)
        {
            return GetPages().FindAll(p => p.PluginId == PluginId);
        }

        #region PRIVATE METHODS
        /// <summary>
        /// Constructs the page objects from the data store data
        /// </summary>
        private static List<PageInfo> FillPages(IDataReader dr)
        {
            List<PageInfo> list = new List<PageInfo>();
            while (dr.Read())
            {
                list.Add(ActualRead(dr));
            }
            dr.Close();
            return list;
        }

        /// <summary>
        /// The actual read
        /// </summary>
        private static PageInfo ActualRead(IDataReader dr)
        {
            PageInfo page = new PageInfo();
            if (dr["PageId"] != Convert.DBNull)
                page.PageId = (int)dr["PageId"];
            if (dr["Name"] != Convert.DBNull)
                page.Name = (string)dr["Name"];
            if (dr["Title"] != Convert.DBNull)
                page.Title = (string)dr["Title"];
            if (dr["PluginId"] != Convert.DBNull)
                page.PluginId = (int)dr["PluginId"];
            return page;
        }
        #endregion
    }
}
